﻿using Helper;
using Helper.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Starehe.ViewModels
{
    public class NewStaffVM: ViewModelBase
    {
        StaffModel newStaff;
        public NewStaffVM()
            : base()
        {
            InitVars();
            CreateCommands();
        }
        protected override void CreateCommands()
        {
            
            SaveCommand = new RelayCommand(async o => { bool succ = await DataAccess.SaveNewStaffAsync(newStaff); if (succ)Reset(); }, o => ValidateStaff());
            ClearImageCommand = new RelayCommand(o => { newStaff.SPhoto = null; }, o => true);
            BrowseCommand = new RelayCommand(o => { newStaff.SPhoto = FileHelper.BrowseImageAsByteArray(); }, o => true);
        }

        protected override async void InitVars()
        {
            
            newStaff = new StaffModel();
            newStaff.StaffID = await DataAccess.GetNewStaffIDAsync();
            Title = "NEW STAFF MEMBER";
        }
        public StaffModel NewStaff
        {
            get { return newStaff; }
        }
        public ICommand SaveCommand
        {
            get;
            private set;
        }
        public ICommand ClearImageCommand
        {
            get;
            private set;
        }
        public ICommand BrowseCommand
        {
            get;
            private set;
        }
        public async override void Reset()
        {
            newStaff.Reset();
            newStaff.StaffID = await DataAccess.GetNewStaffIDAsync();
        }
        private bool ValidateStaff()
        {
            bool isOk = newStaff.StaffID>0
                && !string.IsNullOrWhiteSpace(newStaff.Name) && !string.IsNullOrWhiteSpace(newStaff.NationalID)
                && !string.IsNullOrWhiteSpace(newStaff.Email) && !(newStaff.DateOfAdmission == null)
                   && !string.IsNullOrWhiteSpace(newStaff.PhoneNo) && !string.IsNullOrWhiteSpace(newStaff.Address)
                    && !string.IsNullOrWhiteSpace(newStaff.City) && !string.IsNullOrWhiteSpace(newStaff.PostalCode)
                      && (EmailValidator.IsValidEmail(newStaff.Email));
            return isOk;
        }
    }
}
